from game_helpers import Game

#=======================================================================================================================
# GameStorage Class
#
# Store all game's here.  add_game() should be only be called by a separate entity, like a manager.
#=======================================================================================================================
class GameStorage(object):
    def __init__(self):
        self.games = {}

    def _get_player_id(self, game_id, player_id):
        if self.games[game_id].player_1.id == player_id:     
            return 1
            
        elif self.games[game_id].player_2.id == player_id:   
            return 2
            
        else: 
            raise
    
    def add_game(self, game_id):
        self.games[game_id] = Game(game_id)
        print "Manager added GAME: {0}.".format(game_id)
        
    def set_player_for_game(self, game_id, player_id):
        self.games[game_id].set_player( player_id )
        print "PLAYER: {0} entered GAME: {1}.".format( player_id, game_id )
    
    def place_boat_for_player(self, game_id, player_id, length, x_coord, y_coord, orientation):
        id = self._get_player_id( game_id, player_id )
        
        if id == 1: self.games[game_id].place_boat( 1, length, x_coord, y_coord, orientation )
        elif id == 2: self.games[game_id].place_boat( 2, length, x_coord, y_coord, orientation )
    
    def shoot(self, game_id, player_id, x_coord, y_coord):
        ''' The player_id is the id of the player shooting, not the one being shot at
            This will be taken care by the Game class                                  ''' 
        id = self._get_player_id( game_id, player_id )
        hit = False
        
        if id == 1: hit = self.games[game_id].shoot( 1, x_coord, y_coord )
        if id == 2: hit = self.games[game_id].shoot( 2, x_coord, y_coord )
        
        return hit
    
    def get_state_for_game(self, game_id):
        return self.games[game_id].state.get_state()
    
    def snapshot(self, game_id):
        print "\n---------------"
        print "[PLAYER {0}]".format(self.games[game_id].player_1.id)
        print "[W/L]", self.games[game_id].player_1.wins, "/", self.games[game_id].player_1.losses
        print "[SHOTS TAKEN]", self.games[game_id].player_1.shots_taken
        print "\n---------------"
        print "[PLAYER {0}]".format(self.games[game_id].player_2.id)
        print "[W/L]", self.games[game_id].player_2.wins, "/", self.games[game_id].player_2.losses
        print "[SHOTS TAKEN]", self.games[game_id].player_2.shots_taken
          
    def did_i_win(self, game_id, player_id):
        return self.games[game_id].winner == player_id
    
    def win_loss_count(self, game_id, player_id):
        id = self._get_player_id( game_id, player_id )
        
        if id == 1: 
            wins = self.games[game_id].player_1.wins
            losses = self.games[game_id].player_1.losses
            
        if id == 2: 
            wins = self.games[game_id].player_2.wins
            losses = self.games[game_id].player_2.losses
        
        return (wins, losses)
    
    def get_opponents_last_game_placement(self, game_id, player_id):
        id = self._get_player_id(game_id, player_id)
        
        if id == 1:
            return self.games[game_id].player_2.last_game_board.boat_cells
        elif id == 2:
            return self.games[game_id].player_1.last_game_board.boat_cells
        else:
            raise
    
    def get_opponents_last_game_shots(self, game_id, player_id):
        id = self._get_player_id(game_id, player_id)
        
        if id == 1:
            return self.games[game_id].player_2.last_game_shot_distribution
        elif id == 2:
            return self.games[game_id].player_1.last_game_shot_distribution
        else:
            raise
    
    def get_avg_shots_taken(self, game_id, player_id):
        id = self._get_player_id(game_id, player_id)

        if id == 1:
            return self.games[game_id].player_1.avg_shots_taken
        elif id == 2:
            return self.games[game_id].player_2.avg_shots_taken
        else:
            raise
    
        
        
        